package summary;

import java.util.*;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/12 9:24
 * https://leetcode.cn/problems/course-schedule-iv/
 */
public class Title1462 {

    public static List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
        //<i, j...k> 逆邻接表
        Map<Integer, List<Integer>> graph = new HashMap<>();
        //前置课程集合
        Map<Integer, Set<Integer>> preGraph = new HashMap<>();
        for (int i = 0; i < numCourses; i++) {
            graph.put(i, new LinkedList<>());
            preGraph.put(i, new HashSet<>());
        }
        for (int[] pre : prerequisites) {
            graph.computeIfAbsent(pre[1], g -> new ArrayList<>()).add(pre[0]);
        }
        boolean[] visited = new boolean[numCourses];
        //递归查找每门课程的所有前置课程
        for (int i = 0; i < numCourses; i++) {
            if (!visited[i]) {
                dfs(graph, preGraph, visited, i);
            }
        }
        List<Boolean> result = new ArrayList<>();
        for (int[] query : queries) {
            result.add(preGraph.getOrDefault(query[1], new HashSet<>()).contains(query[0]));
        }
        return result;
    }

    private static void dfs(Map<Integer, List<Integer>> graph, Map<Integer, Set<Integer>> preGraph, boolean[] visited, int cur) {
        visited[cur] = true;
        for (Integer pre : graph.get(cur)) {
            if (!visited[pre]) {
                dfs(graph, preGraph, visited, pre);
            }
            preGraph.get(cur).add(pre);
            preGraph.get(cur).addAll(preGraph.get(pre));
        }
    }


    public static void main(String[] args) {
        Map<Integer, List<Integer>> hash = new HashMap<>();
        hash.getOrDefault(1, new ArrayList<>()).add(2);
        System.out.println();
    }
}
